<template>
	<view>
		<tn-nav-bar fixed>视频上传</tn-nav-bar>
		<view class="content">
			<view class="example-body">
				<video v-if="videoPath" :src="videoPath" object-fit="cover"></video>
				<u-empty v-else text="请上传视频" mode="data"></u-empty>
				<tn-button size="lg" @tap="subVideo" shape="default" backgroundColor='#ff589b'
					margin="30rpx 0 0 230rpx" :shadow="true" fontColor="white">上传视频</tn-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value: '我们',
				videoPath:'',
			}
		},
		methods: {
			subVideo() {
				let that = this;
				uni.chooseVideo({
					count: 1,
					a: that,
					success: (url) => {
						const videoPath = url.tempFilePath;
						console.log(videoPath);
						this.videoPath = videoPath
						uni.uploadFile({
							url: 'http://114.55.37.219:9090/files/upload',
							filePath: videoPath,
							name: 'file',
							value: this.value,
							formData: {
								title: this.value
							},
							header: {
								'token': uni.getStorageSync('token')
							},
							success: (res) => {
								console.log(res);
								uni.setStorageSync('videoUrl',res.data)
								this.sendVideoUrl()
								uni.showToast({
									icon: 'success',
									title: "视频上传成功"
								})
								
							},
							fail: () => {
								uni.showToast({
									icon: 'error',
									title: '视频上传失败'
								})
							}
						})
					}
				})
			},
			async sendVideoUrl(){
				let username = uni.getStorageSync('username')
				let token = uni.getStorageSync('token')
				let videoUrl = uni.getStorageSync('videoUrl')
				let res = await this.request({
					url:'/video',
					data:{
						username,
						videoUrl
					},
					header:{
						token
					},
					method:'POST'
				})
				console.log(res);
			}

		}
	}
</script>

<style>
	.content{
		position: relative;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		margin: auto;
		margin-top: 100rpx;
		width: 90%;
		background-color: white;
	}
	.example-body {
		padding: 10px;
		padding-top: 0;
		
		video{
			margin: auto;
		}
	}

	.custom-image-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.text {
		font-size: 14px;
		color: #333;
	}
</style>